jQuery.fn.onPositionChanged = function (trigger, millis) {
    if (millis == null) millis = 100;
    var o = $(this[0]); // our jquery object
    if (o.length < 1) return o;

    var lastPos = null;
    var lastOff = null;
    setInterval(function () {
        if (o == null || o.length < 1) return o; // abort if element is non existend eny more
        if (lastPos == null) lastPos = o.position();
        if (lastOff == null) lastOff = o.offset();
        var newPos = o.position();
        var newOff = o.offset();
        if (lastPos.top != newPos.top || lastPos.left != newPos.left) {
            $(this).trigger('onPositionChanged', { lastPos: lastPos, newPos: newPos });
            if (typeof (trigger) == "function") trigger(lastPos, newPos);
            lastPos = o.position();
        }
        if (lastOff.top != newOff.top || lastOff.left != newOff.left) {
            $(this).trigger('onOffsetChanged', { lastOff: lastOff, newOff: newOff});
            if (typeof (trigger) == "function") trigger(lastOff, newOff);
            lastOff= o.offset();
        }
    }, millis);
    return o;
};

function toggleFixed () {
	//$("#myScrollspy").css("display","auto");
	
	var parentwidth = $(".col-md-3").width();
	$("#myScrollspy").toggleClass("fixed").width(parentwidth);
}
if(toc=='true'){
	//function Clean () {$("#myScrollspy").css("display","none");}
	toggleFixed();
	//window.onload=function(){
	var sta=["0"];
	var arr=$("#main").children("h1,h2,h3,h4,h5,h6");
	var mmin="998244353"
	if($("#main").children("h1").length)mmin="1";
	else if($("#main").children("h2").length)mmin="2";
	else if($("#main").children("h3").length)mmin="3";
	else if($("#main").children("h4").length)mmin="4";
	else if($("#main").children("h5").length)mmin="5";
	else if($("#main").children("h6").length)mmin="6";
	console.log(mmin);
	var orzzlc=["","","&nbsp;","&nbsp;&nbsp;","&nbsp;&nbsp;&nbsp;","&nbsp;&nbsp;&nbsp;&nbsp;","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"]
	var str='';
	for(var i=0;i<arr.length;i++){
		//console.log(arr[i]);
		var id=arr[i].tagName[1];
		while(sta[sta.length-1]>id){
			str=str+"</li>\n";
			str=str+"</ul>\n";
			sta.pop();
		}
		if(sta[sta.length-1]==id){
			str=str+"</li>\n";
			str=str+"<li >\n";
			str=str+"<a class='more' href=\"#"+arr[i].id+"\">"+orzzlc[id]+arr[i].innerHTML.replace("<a","<span").replace("a>","span>")+"</a>\n";
		}
		else{
			if(id==mmin)str=str+'<ul class="nav nav-tabs nav-stacked">\n';
			else str=str+'<ul class="nav nav-tabs nav-stacked not">\n';
			str=str+"<li>\n";
			str=str+"<a class='more' href=#"+arr[i].id+">"+orzzlc[id]+arr[i].innerHTML.replace("<a","<span").replace("a>","span>")+"</a>\n";
			sta.push(id);
		}
		arr[i].innerHTML+="&nbsp;<small><a class='fa fa-paperclip' href='#"+arr[i].id+"'></i></small>";
	}
	while(sta[sta.length-1]>"0"){
		str=str+"</li>\n";
		str=str+"</ul>\n";
		sta.pop();
	}
	document.getElementById("myScrollspy").innerHTML=str;
	console.log($("#kk").offset().top-60);
	$('body').scrollspy({ target: '#myScrollspy'});
	$("#myScrollspy").affix({
	    offset: { 
	        top: $("#kk").offset().top-50
			//top:998244353
		}
	});
	/*$("#kk").onPositionChanged(function(){
		console.log($('#kk').offset().top - $(window).scrollTop());
		if($('#kk').offset().top - $(window).scrollTop()<60){
			$("#myScrollspy").addClass("affix");
			$("#myScrollspy").removeClass("affix-top");
		}else{
			$("#myScrollspy").removeClass("affix");
			$("#myScrollspy").addClass("affix-top");
		}
	});
	*/
	toggleFixed();
	//}
	
	
	
	window.onresize = function(){
		toggleFixed();
	}
}